const path = require('path');

const express = require('express');

const errorController = require('./controllers/error');
const createMongoClient = require('./utils/database').createMongoClient;
const User = require('./modules/user');

const app = express();

app.set('view engine', 'ejs');
app.set('views', 'views');

const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use((req, res, next) => {
    User.findById('6246a8da6c40ed83d67fbdde')
    .then(user => {
        req.user = new User(user._id, user.name, user.email, user.cart);
        next();
    })
    .catch(err => console.log(err))
})

app.use('/admin', adminRoutes);
app.use(shopRoutes);

app.use('/', errorController.get404);

createMongoClient(() => {
    app.listen(443);
})